﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models;
using System.Web.Security;

namespace WebUI.Controllers
{
    public class MobileController : _SiteBaseController
    {
        //
        // GET: /Mobile/
        public ActionResult Index()
        {
            if (SiteUser == null || SiteUser.MainRoleName != "Recuper")
            {
                return RedirectToAction("LogOn");
            }

            var u =SiteUser;
            var uid = u.UserId;
            var m = DB.QizSubjectObjForDoctors.Where(v => v.Recuper.UserID == uid).Select(v => v.QizSubject).Distinct();
            ViewBag.Hospitals = DB.QizSubjectObjForDoctors.Where(v => v.Recuper.UserID == uid && v.QizSubject != null).Select(v => v.QizSubject).Distinct().Select(s => s.Hospital.HospName).Distinct().ToList();


            return View(m);
        }
        [HttpGet]
        public ActionResult LogOn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    
                    return RedirectToAction("Index");
                    
                }
                else
                {
                    ModelState.AddModelError("", "用户名或密码错误.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        [HttpGet]
        public ActionResult QizSessionEdit(string SubjectID, string ID, string RefAttID)
        {
            var m = Services.QizSessionEditInner_Get(SubjectID, ID, RefAttID);
            return ViewOrPartial(m);
        }


        [HttpPost]
        public ActionResult QizSessionEdit(QizSession tm)
        {
            var m = Services.QizSessionEditInner_Post(tm);
            if (m.Item2)
                return RedirectToAction("QizSessionEdit", new { PreviewOnly = true, ID = m.Item1.ID });
            else
                return View(m.Item1);
        }


    }
}
